package com.example.zyfyptlzx729.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zyfyptlzx729.R;
import com.example.zyfyptlzx729.adapter.TcaseAdapter;
import com.example.zyfyptlzx729.bean.ArticleBean;
import com.example.zyfyptlzx729.iface.ArticleListener;
import com.example.zyfyptlzx729.model.ArticleModel;
import java.util.List;

public class Fragment41 extends BaseFragment {
    private int page=1;
    private List<ArticleBean> list;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private TcaseAdapter adapter;
    private int lastVisibleltemPostion;
    private ArticleListener listener=new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> beanList) {
            if(page==1){
                list=beanList;
            }
            else{
                list.removeAll(beanList);
                list.addAll(beanList);
            }
            adapter.setList(list);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context,"失败："+msg,Toast.LENGTH_SHORT).show();
        }
    };
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment41,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        System.out.println("--f41--"+getSessionId());
        initRecyclerView(view);
        ArticleModel model=new ArticleModel();
        model.getResultList(getSessionId(),"tcase",1,  listener);
    }

    private void initRecyclerView(View view) {
        recyclerView=view.findViewById(R.id.recyclerview);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        adapter=new TcaseAdapter(context);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(newState==RecyclerView.SCROLL_STATE_IDLE&&lastVisibleltemPostion+1==list.size()){
                    page+=1;
                    ArticleModel model=new ArticleModel();
                    model.getResultList(getSessionId(),"tcase",1,  listener);

                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleltemPostion=layoutManager.findLastCompletelyVisibleItemPosition();
            }
        });
    }
}
